package cn.dreamblog.controller;

import cn.dreamblog.pojo.Blog;
import cn.dreamblog.pojo.User;
import cn.dreamblog.service.BlogService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 林泽豪
 * @create 2019-11-05 16:45
 */
@Controller
@RequestMapping("/blog")
public class BlogController {

    @Resource
    BlogService blogService;

    @RequestMapping("/list.do")
    @ResponseBody
    public String queryBlogList(Blog blog, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        int pageIndex=1;
        if (request.getParameter("pageIndex")!=null&&request.getParameter("pageIndex").length()>0) {
            pageIndex=Integer.parseInt(request.getParameter("pageIndex"));
        }
        PageHelper.startPage(pageIndex, 7);
        List<Blog> blogList = blogService.queryBlogList(blog);
        PageInfo<Blog> pageInfo = new PageInfo<>(blogList);
        if (blogList != null) {
            map.put("pageInfo", pageInfo);
        }
        return JSON.toJSONString(map);
    }

}
